//
//  ClubDesViewCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/3.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ClubDesViewCell.h"
#import "AddressField.h"
#import <UIButton+WebCache.h>

@interface ClubDesViewCell ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet AddressField *desTextView;
@property (weak, nonatomic) IBOutlet UIButton *imageBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;


@end

@implementation ClubDesViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.desTextView.placeHolder = @"请输入图片描述";
    });

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    self.addBtn.backgroundColor = [UIColor grayColor];
}

- (void)setModel:(ClubEditModel *)model
{
    _model = model;
    if (model.desImage) {
        [self.imageBtn setBackgroundImage:model.desImage forState:UIControlStateNormal];
    }else {
        if (model.desImageStr) {
            [self.imageBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:model.desImageStr] forState:UIControlStateNormal placeholderImage:GetImage(@"ic_edit_club_upload_img")];
        }else {
            [self.imageBtn setBackgroundImage:GetImage(@"ic_edit_club_upload_img") forState:UIControlStateNormal];
        }
    }
    if (model.desStr) {
        self.desTextView.text = model.desStr;
    }else {
        self.desTextView.text = @"";
    }
    if (model.desStr) {
        self.addBtn.enabled = YES;
        self.addBtn.backgroundColor = RGBColor(255, 120, 201);
    }else {
        self.addBtn.enabled = NO;
        self.addBtn.backgroundColor = [UIColor grayColor];
    }
    self.desTextView.tag = model.textViewtag;
    if (model.addBtnTag == 1) {
        [self.addBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.addBtn.tag = model.addBtnTag;
    }else {
        [self.addBtn setTitle:@"新增" forState:UIControlStateNormal];
        self.addBtn.tag = 0;
    }
}
- (IBAction)upLoadClcik:(UIButton *)btn
{
    if (self.upLoadPhoto) {
        self.upLoadPhoto(btn,self.model,self.desTextView.text.length>0?1:0,self.addBtn);
    }
}
- (IBAction)addDeleteClick:(UIButton *)btn
{
    if (self.addDeleteBlock) {
        self.addDeleteBlock(btn,self.model);
    }
}
- (void)textChanged:(NSNotification *)noti
{
    AddressField *textView = noti.object;
    if (textView.tag == self.model.textViewtag) {
        self.model.desStr = textView.text;
        if (textView.text.length>0 && self.model.desImageStr.length>0) {
            self.addBtn.backgroundColor = RGBColor(255, 120, 201);
            self.addBtn.enabled = YES;
        }else {
            self.addBtn.backgroundColor = [UIColor grayColor];
            self.addBtn.enabled = NO;
        }
    }
}



@end
